package com.zzld.modules.autoinn.action;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.abel533.entity.Example;
import com.github.pagehelper.PageInfo;
import com.zzld.core.utils.Base64Coder;
import com.zzld.core.utils.CookieUtils;
import com.zzld.core.utils.EmojiFilter;
import com.zzld.core.utils.UuidUtil;
import com.zzld.modules.autoinn.entity.Coupon;
import com.zzld.modules.autoinn.entity.Customer;
import com.zzld.modules.autoinn.entity.Facilitator;
import com.zzld.modules.autoinn.service.ICustomerService;
import com.zzld.modules.autoinn.service.IExpenseService;
import com.zzld.modules.autoinn.service.IFacilitatorService;
import com.zzld.modules.common.dto.CommonJSON;
import com.zzld.modules.maintenance.entity.Matnexpense;
import com.zzld.modules.maintenance.service.IMatnexpenseService;
import com.zzld.modules.maintenance.service.IMatnexpensepathService;
import com.zzld.modules.wechat.WeChat;
import com.zzld.modules.wechat.bean.UserInfo;
import com.zzld.modules.wechat.oauth.Oauth;
import com.zzld.modules.wechat.oauth.User;
import com.zzld.modules.wxsys.entity.WXAccount;
import com.zzld.modules.wxsys.entity.WXaccesstoken;
import com.zzld.modules.wxsys.service.IWXAccesstokenService;
import com.zzld.modules.wxsys.service.IWXAccountService;

@Controller
@RequestMapping("/moblile/index")
public class WXMobileController {

	@Autowired
	private IWXAccountService wxAccountService;

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IWXAccesstokenService wxaccesstokenService;

	@Autowired
	private IMatnexpensepathService expensepathService;

	@Autowired
	private IExpenseService expenseService;
	
	@Autowired
	private IMatnexpenseService mtexpenseService;

	@Autowired
	private IFacilitatorService facilitatorService;

	private String indexurl = "/wx/mobile/index";
	private String auindexurl = "/wx/mobile/auindex";
	private String mainurl = "/wx/mobile/main";
	private String loginurl = "/wx/mobile/login";
	private String detailurl="/wx/mobile/detail";

	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, HttpSession session,
			ModelMap modelmap, Model model, HttpServletResponse response) {

		try {
			String storeid = CookieUtils.getCookie(request, "storeid");
			if (storeid != null) {
				return indexurl;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return loginurl;
	}

	@RequestMapping("/auindex")
	public String auindex(HttpServletRequest request, HttpSession session,
			ModelMap modelmap, Model model, HttpServletResponse response) {

		try {
			String storeid = CookieUtils.getCookie(request, "storeid");
			if (storeid != null) {
				return auindexurl;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return loginurl;
	}

	@RequestMapping("/main")
	public String main(HttpServletRequest request, HttpSession session,
			ModelMap modelmap, Model model, HttpServletResponse response) {
		String openid = CookieUtils.getCookie(request, "autoinn_session");
		String wxaccount = CookieUtils.getCookie(request, "wx_session");
		try {
			openid = Base64Coder.decode(openid);
			wxaccount = Base64Coder.decode(wxaccount);
			Customer entity = this.customerService.findCustomer(wxaccount,
					openid);
			if (entity != null) {
				Example example = new Example(Facilitator.class);
				example.createCriteria().andEqualTo("wx_openid", openid);
				if (facilitatorService.selectByExample(example).size() > 0) {

					CookieUtils.setCookie(response, "storeid",
							facilitatorService.selectByExample(example).get(0)
									.getStoreid());

					return mainurl;

				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return loginurl;
	}
	
	@RequestMapping("/detail")
	public String detail(Model model,HttpServletRequest request){
	    String id = request.getParameter("id");

			if (StringUtils.isNotBlank(id)) {

				List<Matnexpense> list = mtexpenseService.selectbydetail(id);
				if (list.size() > 0) {
					model.addAttribute("list", list);
				}
			}

		
		    return detailurl;
	}

	@RequestMapping("/login")
	public String login() {
		return loginurl;
	}
	
	

	@RequestMapping("/aulist")
	@ResponseBody
	public CommonJSON aulist(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		Map<String, Object> map = new HashMap<String, Object>();
		String storeid = CookieUtils.getCookie(request, "storeid");
		map.put("sort", "innertime");
		map.put("order", "DESC");
		String sou = request.getParameter("sou");
		if (!sou.equals("")) {
			map.put("phoneNum", sou);
		} else {
			map.put("phoneNum", null);
		}
		
		String innertime = request.getParameter("innertime");
		//1当日记录2当月记录3全部记录
		if(!innertime.equals("")){
			switch(innertime){
			case "1":
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				map.put("innertime", sdf.format(new Date()));
				break;
			case "2":
				SimpleDateFormat sdd = new SimpleDateFormat("yyyy-MM");
				map.put("innertime", sdd.format(new Date()));
				break;
			case "3":
				map.put("innertime", null);
				break;
				default:
					break;
			}
		}

		map.put("storeid", storeid);
		Integer PageNum = Integer.parseInt(request.getParameter("pageNum"));
		Integer PageSize = 5;

		PageInfo page = expenseService.list(map, PageNum, PageSize);
		json.setRows(page.getList());
		json.setTotal(page.getTotal());
		json.setSuccess(true);
		return json;
	}

	@RequestMapping("/pagelist")
	@ResponseBody
	public CommonJSON pagelist(HttpServletRequest request) {
		CommonJSON json = new CommonJSON();
		String storeid = CookieUtils.getCookie(request, "storeid");
		Map<String, Object> map = new HashMap<String, Object>();
		String sou = request.getParameter("sou");
		if (!sou.equals("")) {
			map.put("phonenum", sou);
		} else {
			map.put("phonenum", null);
		}
		map.put("storeid", storeid);
		Integer PageNum = Integer.parseInt(request.getParameter("pageNum"));
		Integer PageSize = 5;

		PageInfo pageinfo = expensepathService.listbyfa(map, PageNum, PageSize);

		json.setRows(pageinfo.getList());
		json.setTotal(pageinfo.getTotal());
		json.setSuccess(true);

		return json;
	}
}
